package com.logistics.distribution;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.logistics.database.Distributiondatabase;

public class Stockwithgoodsservlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Stockwithgoodsservlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String type = new String(request.getParameter("type").getBytes("iso-8859-1"),"utf-8");		
		System.out.println("type="+type);
		if(type.equals("stock"))
		{
			Distributiondatabase ddb0=new Distributiondatabase();
			String[] goods=request.getParameterValues("goods");
			if(goods!=null)
			{
				System.out.println("goods length="+goods.length);
				for(int i=0;i<goods.length;i++)
				{				
					String number=request.getParameter(goods[i]);
					
					System.out.println("goods["+i+"]="+goods[i]+" number="+number);
					
					ddb0.inoutstoreroomInsert(goods[i], number);
					ddb0.lacksDelete(goods[i]);
				}
			}
		}
		String product_name= new String(request.getParameter("product_name").getBytes("iso-8859-1"),"utf-8");			
		String datestr= new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		ArrayList<Product> products=new ArrayList<Product>();
		Distributiondatabase ddb1=new Distributiondatabase();
		ResultSet rs1=ddb1.productsQuery();
		try {
			while(rs1.next())
			{
				Product pd=new Product();				
				pd.setProduct_id(Integer.toString(rs1.getInt(1)));				
				pd.setProduct_name(rs1.getString(2));
				if(product_name.length()!=0&&!product_name.equals(pd.getProduct_name())) continue;
				pd.setCategory_lv1(rs1.getString(3));
				pd.setCategory_lv2(rs1.getString(4));
				pd.setUnit(rs1.getString(5));
				pd.setPrice(Double.toString(rs1.getDouble(6)));
				pd.setDiscount(Double.toString(rs1.getDouble(7)));
				pd.setPrime_cost(Double.toString(rs1.getDouble(8)));
				pd.setType(rs1.getString(9));
				pd.setSupplier_name(rs1.getString(10));
				pd.setProducer(rs1.getString(11));
				pd.setQg_period(rs1.getString(12));
				pd.setTips(rs1.getString(13));
				pd.setCan_return(Integer.toString(rs1.getInt(14)));
				pd.setWarning_number(Integer.toString(rs1.getInt(15)));
				pd.setMax_number(Integer.toString(rs1.getInt(16)));
				
				int temp=0;
				Distributiondatabase ddb2=new Distributiondatabase();
				ResultSet rs2=ddb2.inoutstoreroomQuery(pd.getProduct_id());
				Distributiondatabase ddb3=new Distributiondatabase();
				ResultSet rs3=ddb3.stockQuery(pd.getProduct_id());
				Distributiondatabase ddb4=new Distributiondatabase();
				ResultSet rs4=ddb4.lacksQuery(pd.getProduct_id());
				while(rs2.next())
				{
					temp+=Integer.parseInt(rs2.getString(1));
				}
				while(rs3.next())
				{
					temp+=rs3.getInt(1);
				}
				pd.setNownumber(temp);
				temp=Integer.parseInt(pd.getWarning_number())-pd.getNownumber();
				while(rs4.next())
				{
					temp+=rs4.getInt(1);
				}
				if(temp<0) temp=0;
				pd.setLessnumber(temp);
				products.add(pd);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		
		
		for(int i=0;i<products.size()-1;i++)
			for(int j=i+1;j<products.size();j++)
			{
				if(products.get(i).getLessnumber()<products.get(j).getLessnumber())
				{
					Product pd=new Product(products.get(i));
					products.set(i, products.get(j));
					products.set(j, pd);
				}
			}
		// sort products by lessnumber
		
		request.setAttribute("datestr", datestr);
		request.setAttribute("products", products);
		request.getRequestDispatcher("../distribution/Stockwithgoods.jsp").forward(request, response); 
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
